package com.jiyun.task;

import com.jiyun.common.RedisContact;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Set;

@Component
public class MyJob {

    @Autowired
    RedisTemplate redisTemplate;


    @Scheduled(cron = "0/5 * * * * *")
    public void myJob(){
        System.out.println("定时任务执行中。。。。");
        // 查询所有消息中过期的消息
        ZoneId zone = ZoneId.systemDefault();
        long l = LocalDateTime.now().atZone(zone).toInstant().toEpochMilli();
        // 从0到当前时间的范围内的数据，都是已经过期的数据
        Set<String> set = redisTemplate.boundZSetOps(RedisContact.ZSETMSG).rangeByScore(0, l);
        for (String id : set) {
            // 根据过期消息的id，删除过期消息的本体
            redisTemplate.boundHashOps(RedisContact.HASHMSG).delete(id);
        }
    }

}
